/**
 * 
 */
package com.laozhao.EasySales.dialog;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.BorderUIResource;

import com.laozhao.EasySales.business.MasterServer;
import com.laozhao.EasySales.control.master.SysSqlAction;
import com.laozhao.EasySales.model.entity.SysSql;
import com.laozhao.dreamframe.util.FrameFactory;
import com.laozhao.dreamframe.util.ToolsUtil;

/**
 * @author LaoZhao
 * 
 */
public class SysSqlDialog extends AbstractDialog<SysSql> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1116226122928664491L;
	
	private JLabel sqlIdLab = new JLabel("sql_id");
	private JTextField sqlIdTxt = new JTextField();
	private JLabel sqlValueLab = new JLabel("sql_Value");
	private JTextField sqlValueTxt = new JTextField();
	private JLabel sqlDesLab = new JLabel("sql_Description");
	private JTextField sqlDesTxt = new JTextField();

	public SysSqlDialog() {
		super();
		initComponents();
		initEvent();
	}
	
	private void initEvent() {
		saveButton.setActionCommand("Save");
		keepButton.setActionCommand("Keep");
//		cancelButton.setActionCommand("Cancel");
		saveButton.addActionListener((ActionListener) FrameFactory.getInstance(SysSqlAction.class));
		keepButton.addActionListener((ActionListener) FrameFactory.getInstance(SysSqlAction.class));
//		cancelButton.addActionListener((ActionListener) FrameFactory.getInstance(SysSqlAction.class));
		
	}

	@Override
	protected void initComponents() {
		super.initComponents();
		
		JPanel mainPanel = new JPanel(new GridLayout(3,2,5,10));
		mainPanel.add(sqlIdLab);
		mainPanel.add(sqlIdTxt);
		mainPanel.add(sqlValueLab);
		mainPanel.add(sqlValueTxt);
		mainPanel.add(sqlDesLab);
		mainPanel.add(sqlDesTxt);
		mainPanel.setBorder( new BorderUIResource.TitledBorderUIResource("SYS_SQL") );

		getContentPane().add(mainPanel,BorderLayout.CENTER);
		
		this.pack();
		repaint();	
		setSize(311,143);
	}

	@Override
	public void init() {
		if (dataModel != null) {
			sqlIdTxt.setText(dataModel.getSqlId());
			sqlValueTxt.setText(dataModel.getSqlValue());
			sqlDesTxt.setText(dataModel.getDescription());
		}else {
			sqlIdTxt.setText("");
			sqlValueTxt.setText("");
			sqlDesTxt.setText("");
		}

		ToolsUtil.setToCenter(FrameFactory.getMainFrame(), this);
		setVisible(true);		
	}
	
	public void insertSql(){
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		insertMap.put("SQL_ID", sqlIdTxt.getText());
		insertMap.put("SQL_VALUE", sqlValueTxt.getText());
		insertMap.put("DESCRIPTION", sqlDesTxt.getText());
		insertMap.put("DEL_FLG", 0);
		insertMap.put("UPDATE_COUNT", 0);
		
		MasterServer mServer = (MasterServer) FrameFactory.getInstance(MasterServer.class);
		mServer.insertSql(insertMap);
		
	}

}
